package httpCall

import (
	"bytes"
	"encoding/json"
	"net/http"
)

//访问api接口函数
func CallApi(url string, method string, intoMap interface{}) (outMap map[string]interface{}, err error) {
	dataJson, err := json.Marshal(intoMap)
	if err != nil {
		return nil, err
	}
	urlBody := bytes.NewBuffer(dataJson)
	client := &http.Client{}
	req, err := http.NewRequest(method, url, urlBody)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/json;charset=UTF-8")
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	decoder := json.NewDecoder(resp.Body)
	if err = decoder.Decode(&outMap); err != nil {
		return nil, err
	}
	return outMap, nil
}
